package com.project.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;

/**
 * @author lyl
 * description:
 */
public class CharFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");

        HttpServletRequest rs = (HttpServletRequest) servletRequest;
        filterChain.doFilter(new MyRequest(rs), servletResponse);
    }


    private class MyRequest extends HttpServletRequestWrapper {
        private HttpServletRequest request1;

        public MyRequest(HttpServletRequest request) {
            super(request);
            this.request1 = request;
        }

        @Override
        public String getParameter(String name) {
            name = request1.getParameter(name);
            if (name.contains("你妈的")) {
                name = name.replace("你妈的", "***");
            }
            return name;
        }
    }
}
